<?php
/**
 * Created by PhpStorm.
 * User: 小蛮哼哼哼
 * Email: 243194993@qq.com
 * Date: 2021/11/17
 * Time: 21:38
 * motto: 现在的努力是为了小时候吹过的牛逼！
 */

declare(strict_types=1);

namespace app\admin\controller;

use app\common\business\Rider as BusObj;
use app\common\lib\Show;
use app\common\lib\Status;
use think\App;

class Rider extends Base
{
    protected BusObj $busObj;

    public function __construct(App $app)
    {
        parent::__construct($app);
        $this->busObj = new BusObj();
    }

    public function success()
    {
        $id = $this->request->param('id', 0, 'intval');
        if ($id === 0) {
            return Show::json_show(Status::status_error, "内部错误");
        }
        try {
            $order = $this->busObj->getBaseById($id);
        } catch (\Exception $e) {
            return Show::json_show(Status::status_error, $e->getMessage());
        }
        if (!$order) {
            return Show::json_show(Status::status_error, "记录不存在");
        }
        if ($order['state'] != 0) {
            return Show::json_show(Status::status_error, "审核状态不正确");
        }
        $update_data = [
            "state" => 1,
            "state_time" => time()
        ];
        try {
            $res = $this->busObj->updateByData($order['id'], $update_data);
        } catch (\Exception $e) {
            return Show::json_show(Status::status_error, $e->getMessage());
        }
        if ($res) {
            return Show::json_show(Status::mini_normal, "操作成功");
        } else {
            return Show::json_show(Status::status_error, "操作失败");

        }
    }

    public function setError()
    {
        $id = $this->request->param('id', 0, 'intval');
        $msg = $this->request->param('msg', '', 'trim,strip_tags');
        if ($id === 0) {
            return Show::json_show(Status::status_error, "内部错误");
        }
        if (empty($msg)) {
            return Show::json_show(Status::status_error, "请输入拒绝原因");
        }
        try {
            $order = $this->busObj->getBaseById($id);
        } catch (\Exception $e) {
            return Show::json_show(Status::status_error, $e->getMessage());
        }
        if (!$order) {
            return Show::json_show(Status::status_error, "记录不存在");
        }
        if ($order['state'] != 0) {
            return Show::json_show(Status::status_error, "审核状态不正确");
        }
        $update_data = [
            "state" => 2,
            "state_time" => time(),
            "state_msg" => $msg
        ];
        try {
            $res = $this->busObj->updateByData($order['id'], $update_data);
        } catch (\Exception $e) {
            return Show::json_show(Status::status_error, $e->getMessage());
        }
        if ($res) {
            return Show::json_show(Status::mini_normal, "操作成功");
        } else {
            return Show::json_show(Status::status_error, "操作失败");
        }
    }

    public function delOpen()
    {
        $id = $this->request->param('id', 0, 'intval');
        $msg = $this->request->param('msg', '', 'trim,strip_tags');
        if ($id === 0) {
            return Show::json_show(Status::status_error, "内部错误");
        }
        if (empty($msg)) {
            return Show::json_show(Status::status_error, "请输入拒绝原因");
        }
        try {
            $order = $this->busObj->getBaseById($id);
        } catch (\Exception $e) {
            return Show::json_show(Status::status_error, $e->getMessage());
        }
        if (!$order) {
            return Show::json_show(Status::status_error, "记录不存在");
        }
        $update_data = [
            "state" => 3,
            "state_time" => time(),
            "state_msg" => $msg
        ];
        try {
            $res = $this->busObj->updateByData($order['id'], $update_data);
        } catch (\Exception $e) {
            return Show::json_show(Status::status_error, $e->getMessage());
        }
        if ($res) {
            return Show::json_show(Status::mini_normal, "操作成功");
        } else {
            return Show::json_show(Status::status_error, "操作失败");
        }
    }
}